/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.BuildingExt;
import org.springblade.modules.admin.vo.BuildingExtVO;
import org.springblade.modules.admin.mapper.BuildingExtMapper;
import org.springblade.modules.admin.service.IBuildingExtService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.vo.BuildingRescueTimeVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Date;

/**
 * 楼盘信息 服务实现类
 *
 * @author ysq
 * @since 2023-08-29
 */
@Service
public class BuildingExtServiceImpl extends BaseServiceImpl<BuildingExtMapper, BuildingExt> implements IBuildingExtService {

	@Override
	public IPage<BuildingExtVO> selectBuildingExtPage(IPage<BuildingExtVO> page, BuildingExtVO buildingExt) {
		return page.setRecords(baseMapper.selectBuildingExtPage(page, buildingExt));
	}

	@Override
	public BuildingRescueTimeVO getBuildingRescueTime(Long buildingId, Date rescueTime) {
		BuildingRescueTimeVO rescueTimeVO = new BuildingRescueTimeVO();
		BuildingExt buildingExt = this.getOne(Wrappers.<BuildingExt>lambdaQuery().eq(BuildingExt :: getBuildingId, buildingId), false);
		if (Func.isEmpty(buildingExt)) {
			return rescueTimeVO;
		}

		if (buildingExt.getRescueTime() == 0) {
			return rescueTimeVO;
		}

		/**
		 * 白天：06：00 —— 18：00
		 * 晚上：18：00 —— 06：00
		 */
		rescueTimeVO.setType(1);
		int rescueHour = DateUtil.hour(rescueTime, true) + DateUtil.minute(rescueTime)/100 + DateUtil.second(rescueTime)/10000;
		if (rescueHour >= 6 && rescueHour <= 18) {
			rescueTimeVO.setRescueUseTime(buildingExt.getRescueDaytime());
		} else {
			rescueTimeVO.setRescueUseTime(buildingExt.getRescueNight());
		}

		return rescueTimeVO;
	}
}
